function SettingsAssistant() {
	/* this is the creator function for your scene assistant object. It will be passed all the 
	   additional parameters (after the scene name) that were passed to pushScene. The reference
	   to the scene controller (this.controller) has not be established yet, so any initialization
	   that needs the scene controller should be done in the setup function below. */
}

SettingsAssistant.prototype.setup = function() {
	try{
		secret_phrase = "admob_toggle";
		this.cookie = new Mojo.Model.Cookie("SageData");
		var data = this.cookie.get();
		if (data) {
			if (data.username && data.password && data.server) {
				username = data.username;
				password = data.password;
				server = data.server;
				sage_instance = data.instance;
				sage_label = data.label;
				https = data.https;
				admobToggle = data.admobToggle;
				if(data.clients) {
					if(data.clients.length <! 1) {
						clients = data.clients;
					} else {
						clients = new Array;
						clients[0] = {
							label: 'Local Server',
							value: 'SAGETV_PROCESS_LOCAL_UI'
						};
					}
				} else {
					clients = new Array;
					clients[0] = {
						label: 'Local Server',
						value: 'SAGETV_PROCESS_LOCAL_UI'
					};
				}
			} else {
				username = '';
				password = '';
				server = '';
				sage_instance = 'SAGETV_PROCESS_LOCAL_UI';
				sage_label = 'Local Server';
				https = 0;
				admobToggle = 'enabled';
				clients = new Array;
				clients[0] = {
					label: 'Local Server',
					value: 'SAGETV_PROCESS_LOCAL_UI'
				};
			}
		} else {
			username = '';
			password = '';
			server = '';
			sage_instance = 'SAGETV_PROCESS_LOCAL_UI';
			sage_label = 'Local Server';
			https = 0;
			admobToggle = 'enabled';
			clients = new Array;
			clients[0] = {
				label: 'Local Server',
				value: 'SAGETV_PROCESS_LOCAL_UI'
			};
			this.firstRun();
		}

		// Setup Application Menu
		this.appMenuAttr = {omitDefaultItems: true};
		this.appMenuModel = {
			visible: true,
			items: [
				Mojo.Menu.editItem,
				{label: "Help", command: 'help-cmd'}
//				{label: "About", command: 'about-cmd'}
		]};
		this.controller.setupWidget(Mojo.Menu.appMenu, this.appMenuAttr, this.appMenuModel);

		this.textUserAtt = {focus: 1, hintText: 'Sage', textCase: Mojo.Widget.steModeLowerCase};
		this.textPassAtt = {hintText: 'Password', textCase: Mojo.Widget.steModeLowerCase};
		this.textServerAtt = {hintText: '192.168.0.1:8080', textCase: Mojo.Widget.steModeLowerCase, modifierState: Mojo.Widget.numLock};
		this.listselectorClientAtt = {};
		this.listselectorHttpsAtt = {};
		this.buttonClientUpdateAtt = {label: 'Update Client List', type: Mojo.Widget.activityButton};
//		this.clearCookieAtt = {};

		this.textUserModel = {value: username, disabled: false};
		this.textPassModel = {value: password, disabled: false};
		this.textServerModel = {value: server, disabled: false};
		this.listselectorClientModel = {value: sage_instance, choices: clients, disabled: false};
		this.listselectorHttpsModel = {value: https, disabled: false, choices: [
				{label: 'HTTP',	value: '0'},
				{label: 'HTTPS',value: '1'}
			]};
		this.buttonClientUpdateModel = {buttonClass: 'negative'};
//		this.clearCookieModel = {buttonLabel: 'Reset', buttonClass: 'negative', disable: false};
		
		this.controller.setupWidget('spinnerClient', this.spinnerClientAtt, this.spinnerClientModel);
		this.controller.setupWidget('textuser', this.textUserAtt, this.textUserModel);
		this.controller.setupWidget('textpass', this.textPassAtt, this.textPassModel);
		this.controller.setupWidget('textserver', this.textServerAtt, this.textServerModel);
		this.controller.setupWidget('listselectorClient', this.listselectorClientAtt, this.listselectorClientModel);
		this.controller.setupWidget('listselectorHttps', this.listselectorHttpsAtt, this.listselectorHttpsModel);
		this.controller.setupWidget('buttonClientUpdate',this.buttonClientUpdateAtt, this.buttonClientUpdateModel);
//		this.controller.setupWidget('clearCookie', this.clearCookieAtt, this.clearCookieModel);
		
		Mojo.Event.listen(this.controller.get('textuser'), Mojo.Event.propertyChange, this.userUpdate.bind(this));
		Mojo.Event.listen(this.controller.get('textpass'), Mojo.Event.propertyChange, this.passUpdate.bind(this));
		Mojo.Event.listen(this.controller.get('textserver'), Mojo.Event.propertyChange, this.serverUpdate.bind(this));
		Mojo.Event.listen(this.controller.get('listselectorClient'), Mojo.Event.propertyChange, this.clientUpdate.bind(this));
		Mojo.Event.listen(this.controller.get('listselectorHttps'), Mojo.Event.propertyChange, this.httpsUpdate.bind(this));
		Mojo.Event.listen(this.controller.get('buttonClientUpdate'), Mojo.Event.tap, this.buttonClientUpdate.bind(this));
//		Mojo.Event.listen(this.controller.get('clearCookie'), Mojo.Event.tap, this.textClear.bind(this));
	}catch (err) {
		this.showDialogBox('error', err);
	}
}

SettingsAssistant.prototype.storeData = function(event) {
	if(!admobToggle) {
		admobToggle = "enabled";
	}
	this.cookie.put({
		username: username,
		password: password,
		server: server,
		instance: sage_instance,
		label: sage_label,
		https: https,
		admobToggle: admobToggle,
		version: Mojo.Controller.appInfo.version
	});
}

SettingsAssistant.prototype.userUpdate = function(event) {
	username = event.value;
	this.storeData();
}

SettingsAssistant.prototype.passUpdate = function(event) {
	password = event.value;
	this.storeData();
}

SettingsAssistant.prototype.serverUpdate = function(event) {
	server = event.value;
	this.storeData();
}

SettingsAssistant.prototype.httpsUpdate = function(event) {
	https = event.value;
	this.storeData();
	this.secretAdMobToggle();
}

SettingsAssistant.prototype.clientUpdate = function(event) {
	sage_instance = event.value;
	for(i=0;i<clients.length;i++) {
		if(event.value == clients[i].value) {
			sage_label = clients[i].label;
		}
	}
	this.storeData();
}

SettingsAssistant.prototype.textClear = function(event) {
	this.cookie.remove();
}

SettingsAssistant.prototype.fetchClientsCreate = function() {
	this.controller.get('buttonClientUpdate').mojo.activate();
}

SettingsAssistant.prototype.fetchClientsSucc = function(transport) {
	try{
		if(transport.responseText) {
			var temp = transport.responseText.match(/\/\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}[:]\d{1,5}/gi);

			if(temp) {
//				$('test').innerHTML = temp.length + ' servers';
				for(i=0;i<temp.length;i++) {
//					$('test').innerHTML = $('test').innerHTML + '\n' + temp[i];
					clients[1+i] = {
						label: temp[i],
						value: temp[i]
					};
				}
			}
		} else {
			this.fetchClientsFail();
		}
		this.controller.modelChanged(this.listselectorClientModel);
		this.controller.get('buttonClientUpdate').mojo.deactivate();
	} catch (err) {
		this.showDialogBox('error', err);
	}
}

SettingsAssistant.prototype.fetchClientsFail = function(transport) {
	if(username != '' && password != '' && server != '') {
		this.showDialogBox('Connection Error', 'Unable to connect to SageTV Webserver. This may be because it is offline or the username, password, and/or IP address are incorrect. It may have been updated and no longer works with this version of SageTV Remote. This version of SageTV Remote has been tested to work with SageTV Webserver v.2.28.'); // + url);
	}
}

SettingsAssistant.prototype.firstRun = function() {
	this.showDialogBox('Welcome to SageTV Remote', 'Thank you for using SageTV Remote. This webOS app lets you control your SageTV Clients and Media Servers via the SageTV Web Server. Please refer to the help section for more information');
}

SettingsAssistant.prototype.secretAdMobToggle = function(event) {
	// If your reading this then I guess you have figured out what this function does and how to make it work. If you decide to flip this switch, please support inde developers by donating.
	if(username == secret_phrase && password == secret_phrase && server == secret_phrase && https == 1) {
		if(admobToggle == 'enabled') {
			admobToggle = 'disabled';
			this.storeData();
			this.showDialogBox('Info', 'AdMob ads disabled. In order for the change to take effect, you must restart the application.');
		} else {
			admobToggle = 'enabled';
			this.storeData();
			this.showDialogBox('Info', 'AdMob ads enabled. In order for the change to take effect, you must restart the application.');
		}
		return true;
	} else {
		return false;
	}
}

SettingsAssistant.prototype.buttonClientUpdate = function(event) {
	if(https == 1) {
		url = 'https:// ' + username + ':' + password + '@' + server + '/sage/';
	} else {
		url = 'http://' + username + ':' + password + '@' + server + '/sage/';
	}
	
	var ajaxRequest = new Ajax.Request(url + "Home", {
		method: 'get',
		evalJSON: 'false',
		onCreate: this.fetchClientsCreate(),
		onSuccess: this.fetchClientsSucc.bind(this),
		onFailure: this.fetchClientsFail.bind(this),
		on404: this.fetchClientsFail.bind(this),
		on500: this.fetchClientsFail.bind(this)
	});
}

SettingsAssistant.prototype.showDialogBox = function (title, message) {
	this.controller.showAlertDialog({
		onChoose: function (value) {},
		title: title,
		message: message,
		allowHTMLMessage: true,
		choices: [ {label: 'OK', value: 'OK', type: 'color'} ]
	});
};

SettingsAssistant.prototype.activate = function(event) {
	/* put in event handlers here that should only be in effect when this scene is active. For
	   example, key handlers that are observing the document */
	// CSS rules
	$$('body')[0].addClassName('settings');
	$$('body')[0].addClassName('palm-dark');
	$$('body')[0].removeClassName('palm-default');

	this.buttonClientUpdate();
}


SettingsAssistant.prototype.deactivate = function(event) {
	/* remove any event handlers you added in activate and do any other cleanup that should happen before
	   this scene is popped or another scene is pushed on top */
	$$('body')[0].addClassName('palm-default');
	$$('body')[0].removeClassName('settings');
	$$('body')[0].removeClassName('palm-dark');
}

SettingsAssistant.prototype.cleanup = function(event) {
	this.cookie = new Mojo.Model.Cookie("SageData");
	var data = this.cookie.get();
	if (data) {
		if (data.username && data.password && data.server) {
			username = data.username;
			password = data.password;
			server = data.server;
			sage_instace = data.instance;
			url = username + ':' + password + '@' + server + '/sage/';
		}
	}
	$$('body')[0].addClassName('palm-default');
	$$('body')[0].removeClassName('settings');
	$$('body')[0].removeClassName('palm-dark');

}
